﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class YabanShezhiUIController : MonoBehaviour {

    public static YabanShezhiUIController _instance;
    private int i_tempYanbanShezhi;            //选择框的索引值
    private int i_AllUIChildCount;             //ALLUI的child的数量
    private float f_ContentIndex = 1.0f;       //压板设置中内容的变化值
    public Transform tf_AllUI;                 
    public Transform tf_ContentRight;          //压板设置的内容的父集
    public Scrollbar sb_Content;               //压板设置的内容
    private int i_tempValue = 0;               //判断ScrollBar的value能否变化

    // Use this for initialization
    void Start () {
        _instance = this;
        i_AllUIChildCount = tf_AllUI.childCount;
        EventManager<bool>.AddEvent(MyEventType.UpButtonClickEvent, OnUpButtonClick);
        EventManager<bool>.AddEvent(MyEventType.DownButtonClickEvent, OnDownButtonClick);
        EventManager<bool>.AddEvent(MyEventType.SetButtonClickEvent, OnSetButtonClick);
        EventManager<bool>.AddEvent(MyEventType.QuitButtonClickEvent,OnQuitButtonClick);
        EventManager<bool>.AddEvent(MyEventType.ResetButtonClickEvent, ResetYabanShezhi);
        EventManager<bool>.AddEvent(MyEventType.ChongzhiShujuEvent,Reset);
    }

    private void Reset(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yabanshezhiGO") {
            UIController._instance.ChangeGaoliang(tf_ContentRight, i_tempYanbanShezhi, false);
            f_ContentIndex = 1;
            i_tempValue = 0;
            sb_Content.value = f_ContentIndex;
            i_tempYanbanShezhi = 0;
            UIController._instance.ChangeGaoliang(tf_ContentRight, i_tempYanbanShezhi, true);
        }
    }

    private void OnSetButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yabanshezhiGO")
        {
            if (i_tempYanbanShezhi >= 3 && i_tempYanbanShezhi<=5)
            {
                if (tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(0).GetComponent<Text>().text == "退出")
                {
                    tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(0).GetComponent<Text>().text = "投入";

                    Unit unit = new Unit(i_tempYanbanShezhi-2,"touru");
                    EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent,unit);//改变指示灯的状态
                }
                else
                {
                    tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(0).GetComponent<Text>().text = "退出";
                    Unit unit = new Unit(i_tempYanbanShezhi - 2, "tuichu");
                    EventManager<Unit>.TriggerEvent(MyEventType.ZhishidengTypeEvent, unit);
                }
            }
        }
    }

    /// <summary>
    /// 信号复归后压板设置的三段电流状态改为“退出”
    /// </summary>
    /// <param name="active"></param>
    private void ResetYabanShezhi(bool active) {
        for (int index = 3; index <= 5; index++) {
            //tf_ContentRight.GetChild(index).GetChild(0).GetComponent<Text>().text = "";
        }
    }
    private void OnDownButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yabanshezhiGO") {//是否是当前界面
            if (i_tempYanbanShezhi >= 0 && i_tempYanbanShezhi < tf_ContentRight.childCount - 1) {//索引值在区域内
                UIController._instance.ChangeGaoliang(tf_ContentRight, i_tempYanbanShezhi,false);//高亮消失，字体颜色变白
                tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(1).gameObject.SetActive(false);//右箭头消失
                i_tempYanbanShezhi += 1;
                i_tempValue++;
                
                if (i_tempValue >= 4) {
                    i_tempValue = 4;
                }
                UIController._instance.ChangeGaoliang(tf_ContentRight, i_tempYanbanShezhi, true);//高亮出现，字体颜色变灰
                tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(1).gameObject.SetActive(true);//右箭头出现
                //什么情况下才能变化Scrollbar的Value
                if (sb_Content.value > 0 && i_tempValue==4)
                {
                    i_tempValue = 3;
                    f_ContentIndex -= 0.2f;
                    sb_Content.value = f_ContentIndex;
                    if (f_ContentIndex <= 0) {
                        f_ContentIndex = 0;
                    }
                }
            }            
        }
    }

    private void OnUpButtonClick(bool obj)
    {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yabanshezhiGO")
        {
            if (i_tempYanbanShezhi > 0 && i_tempYanbanShezhi<= tf_ContentRight.childCount - 1) {
                UIController._instance.ChangeGaoliang(tf_ContentRight, i_tempYanbanShezhi, false);
                tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(1).gameObject.SetActive(false);//右箭头消失
                i_tempYanbanShezhi -= 1;
                i_tempValue--;
                
                if (i_tempValue <= -1)
                {
                    i_tempValue = -1;
                }
                UIController._instance.ChangeGaoliang(tf_ContentRight, i_tempYanbanShezhi, true);
                tf_ContentRight.GetChild(i_tempYanbanShezhi).GetChild(1).gameObject.SetActive(true);//右箭头出现
                if (sb_Content.value >= 0 && i_tempValue == -1)
                {
                    i_tempValue = 0;
                    f_ContentIndex += 0.2f;
                    sb_Content.value = f_ContentIndex;
                    if (f_ContentIndex >= 1) {
                        f_ContentIndex = 1;
                    }
                }
            } 
        }
    }

    private void OnQuitButtonClick(bool obj) {
        if (tf_AllUI.GetChild(i_AllUIChildCount - 1).name == "yabanshezhiGO") {
            for (int i = 0; i < i_AllUIChildCount; i++)
            {
                if (tf_AllUI.GetChild(i).name == "yunxingshezhiGO")
                {
                    TimerMngr.Instance.Create(() => {
                        EventManager<bool>.TriggerEvent(MyEventType.ChongzhiShujuEvent, true);
                        tf_AllUI.GetChild(i).SetAsLastSibling();
                    }).Start(0.1f);
                    break;
                }
            }
        }
    }
}
